package com.princess.demok.ui.demo

import android.app.Activity
import android.content.Intent
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.princess.baselib.utils.LogUtils
import com.princess.demok.R
import com.princess.demok.base.view.MyBaseActivity
import com.princess.demok.net.data.ImgPointBean
import com.github.chrisbanes.photoview.OnScaleChangedListener
import kotlinx.android.synthetic.main.activity_photoview.*

/**
 * @author: xha
 * @date: 2020/12/21 16:14
 * @Description:
 */
class PhotoViewActivity : MyBaseActivity() {


    companion object {
        @JvmStatic
        fun start(
            context: Activity
        ) {
            val starter = Intent(context, PhotoViewActivity::class.java)
            context.startActivity(starter)
        }
    }

    override fun getLayoutId(): Int? {
        return R.layout.activity_photoview
    }

    override fun initOnCreate() {
        Glide.with(this)
            .load("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2535535235,1109729418&fm=26&gp=0.jpg")
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(ivPhotoview)

        ivPhotoview.setOnPhotoTapListener { _, x, y ->
            LogUtils.i("坐标$x  $y")
//            setClickerCheck(x, y, "", ivPhotoview.scale)
            setImgShow()
        }

        ivPhotoview.setOnScaleChangeListener(object : OnScaleChangedListener {
            override fun onScaleChange(scaleFactor: Float, focusX: Float, focusY: Float) {
                LogUtils.i("缩放改变回调")
                setImgShow()
            }
        })

    }

    private fun setImgShow() {
        val imgPoints: MutableList<ImgPointBean> = arrayListOf()
        for (index in 1..10) {
            val colors = ContextCompat.getColor(
                this,
                R.color.color_009B65
            )
            val imgPointBean =
                ImgPointBean(colors, (index * 0.05).toFloat(), (index * 0.06).toFloat())
            imgPoints.add(imgPointBean)
        }

        ivPhotoview.setScalePoint(true, imgPoints)
    }


    private fun setClickerCheck(x: Float, y: Float, url: String, scale: Float) {


        //缩放级别判断(图片缩放级别越大，对应的点击区域越小)
        val scalePoint = 0.05 * (1 / scale.toDouble())
        LogUtils.i("缩放级别 $scalePoint  图片级别 $scale")
        ivPhotoview.setScalePoint(true, x, y)

    }


}